<?php

$config=array(
      
      'table'=>'List',
      'ziduan'=>array(
                array('title'=>'id','type'=>'int','length'=>'20','kon'=>0,'desc'=>'自增id'),
                array('title'=>'username','type'=>'char','length'=>'20','kon'=>0,'desc'=>'用户名'),
                array('title'=>'short','type'=>'char','length'=>'20','kon'=>1,'desc'=>'排序'),
               ),


    );


//构建模型
$data="<?php
class ".$config['table']."_model extends CI_Model {\n\n";

    foreach ($config['ziduan'] as $key => $value) {
    	$public="    public $".$value['title'].";\n";
    	$data.=$public;
    }


$data.="\n    public function __construct()
    {
        // Call the CI_Model constructor
        parent::__construct();
    }";

$data.=''."\n\n".'    public function get_last_ten_entries()
    {
        $query = $this->db->get("'.$config['table'].'");
        return $query->result();
    }';

$data.=''."\n\n".'    public function insert_entry($data)
    {
        return  $this->db->insert("'.$config['table'].'", $data);
    }';

$data.=''."\n\n".'    public function update_entry($data,$where)
    {
        return  $this->db->update("'.$config['table'].'",$data,$where);
    }';

$data.=''."\n\n".'    public function delete($where)
    {
    	return $this->db->delete("'.$config['table'].'",$where);
    }';


file_put_contents($config['table'].'_model.php',$data);

//构建sql
$db="DROP TABLE IF EXISTS `".$config['table']."`;
CREATE TABLE `".$config['table']."` (\n";

foreach ($config['ziduan'] as $key => $value) {
	$db.="`".$value['title']."` ".$value['type']."(".$value['length'].") ";
	if($value['kon']==0){

		if($value['title']==$config['ziduan'][0]['title']){
			$db.="NOT NULL AUTO_INCREMENT ";
		}else{
			$db.="NOT NULL DEFAULT ''";
		}
		
	}else{
		
		$db.="DEFAULT ''";
	}

	if(!empty($value['desc'])){
		$db.="COMMENT '".$value['desc']."',\n";
	}
	
}
$db.="PRIMARY KEY (`".$config['ziduan'][0]['title']."`)\n";
$db.=") ENGINE=InnoDB DEFAULT CHARSET=utf8;";


echo $db;






